<template>
    <div class="Register">
        <headerCont :page="'register'"></headerCont>
        <div class="main">
            <div class="title">注册</div>
            <div class="cont">
                <div class="cont_li">
                    <div class="cont_li_name">
                        <span>*</span>手机号：
                    </div>
                    <div class="cont_li_input">
                        <a-input type="text" v-model="phone" placeholder="请输入手机号" @keyup.enter.native="submit_register"/>
                    </div>
                    <div class="cont_li_tips">请输入中国大陆手机号，用于登录和修改密码</div>
                </div>
                <div class="cont_li">
                    <div class="cont_li_name">
                        <span>*</span>密码：
                    </div>
                    <div class="cont_li_input">
                        <a-input type="password" v-model="pwd" placeholder="请输入密码" @keyup.enter.native="submit_register"/>
                    </div>
                    <div class="cont_li_tips">8-18位数字英文字母组合，注意区分大小写</div>
                </div>
                <div class="cont_li">
                    <div class="cont_li_name">
                        <span>*</span>确认密码：
                    </div>
                    <div class="cont_li_input">
                        <a-input type="password" v-model="pwd1" placeholder="请再次输入密码" @keyup.enter.native="submit_register"/>
                    </div>
                    <div class="cont_li_tips">确认密码必须与密码保持一致</div>
                </div>
                <div class="cont_li">
                    <div class="cont_li_name">
                        <span>*</span>图片验证码：
                    </div>
                    <div class="cont_li_input captcha">
                        <a-input type="text" v-model="gapcode" placeholder="请输入您看到的图片验证码" @keyup.enter.native="submit_register"/>
                        <span class="pointer" @click="getrandomImage">
                            <img :src="result"/>
                        </span>
                    </div>
                    <div class="cont_li_tips">请输入您看到的图片验证码</div>
                </div>
                <div class="cont_li">
                    <div class="cont_li_name">
                        <span>*</span>手机验证码：
                    </div>
                    <div class="cont_li_input vercode">
                        <a-input type="text" v-model="identify" placeholder="请输入手机验证码" @keyup.enter.native="submit_register"/>
                        <span class="pointer getCodeBtn" id="getCodeBtn" @click="getCode">获取短信验证码</span>
                        <span class="downtimecount" id="downtimecount" style="display: none;"></span>
                    </div>
                    <div class="cont_li_tips">请输入您手机接收到的验证码</div>
                </div>
            </div>
            <div class="resister_btn pointer" @click="submit_register">提交</div>
        </div>
      <footer-cont></footer-cont>
    </div>
</template>

<script>
import headerCont from '@/views/declarationSystem/common/header';
import footerCont from '@/views/declarationSystem/common/footer';
import { randomImage, sbRegister, cxjsb_getYzm } from '@/api/shenbao';
var timer, curCount = 60;
function setCountDown() {
    if (curCount === 0) {
		curCount = 60;
        window.clearInterval(timer);
        document.getElementById('getCodeBtn').style.display="block";
        document.getElementById('downtimecount').style.display="none";
    } else {
        curCount--;
        document.getElementById('downtimecount').style.display="block";
        document.getElementById('getCodeBtn').style.display="none";
        document.getElementById('downtimecount').innerHTML = "请在"+curCount+"秒内输入验证码";
    }
}
    export default {
        name: 'Register',
        data() {
            return {
                reg: /^1[3456789]\d{9}$/,
                phone: '',
                pwd: '',
                pwd1: '',
                gapcode: '',
                identify: '',
                passreg: /^(?=.*[a-zA-Z])(?=.*\d).{8,16}$/,
                key: '',
                result: '',
            }
        },
        components: {
          headerCont,
          footerCont,
        },
        methods: {
            getrandomImage() {
                let that = this;
                this.key = new Date().getTime();
                randomImage(that.key).then(res=>{
                    that.result = res.data.result;
                })
            },
            getCode() {
                let that = this;
                if (!this.phone) {
                    that.$message.warning('请输入手机号');
                    return 
                } 
                if(!this.reg.test(this.phone)) {
                    that.$message.warning('手机号格式不正确');
                    return 
                }
                if(!this.gapcode) {
                    that.$message.warning('请输入图片验证码');
                    return
                }
                cxjsb_getYzm({
                    code: that.gapcode,
                    key: that.key,
                    phoneNumber: that.phone,
                    type: 2
                }).then(res=>{
                    if (res.data.code=='200') {
                        that.$message.success('发送成功');
                        timer = window.setInterval(setCountDown, 1000);
                    } else {
                        that.getrandomImage();
                        that.$message.error(res.data.message);
                    }
                })  
            },
            submit_register() {
                let that = this;
                if (!this.phone) {
                    that.$message.warning('请输入手机号');
                    return 
                } 
                if (!this.pwd || !this.pwd1) {
                    that.$message.warning('请输入密码');
                    return
                }
                if (!this.pwd1) {
                    that.$message.warning('请再次输入密码');
                    return
                }
                if (!this.passreg.test(this.pwd)) {
                    that.$message.warning('密码格式不正确');
                    return
                }
                if (this.pwd!=this.pwd1) {
                    that.$message.warning('两次密码输入不一致');
                    return
                }
                if(!this.gapcode) {
                    that.$message.warning('请输入图片验证码');
                    return
                }
                if (!this.identify) {
                    that.$message.warning('请输入手机验证码');
                    return
                }
                sbRegister({
                    phoneNumber: that.phone, 
                    passWord: that.pwd, 
                    code: that.identify
                }).then(res=>{
                    if (res.data.code=='200') {
                        that.$message.success('注册成功');
                        setTimeout(() => {
                            that.$router.replace({
                                path: '/shenbao/login'
                            })
                        }, 2000);
                    } else {
        			    that.getrandomImage();
                        that.$message.warning(res.data.message);
                    }
                })
            },
        },
        mounted() {
            this.getrandomImage();
        }
    }
</script>

<style lang="less" scoped>
.Register {
  background-color: #f7f7f7;
}
.main {
    width: 900px;
	  margin: 0 auto;
    min-height: 460px;
    background-color: #fff;
    padding: 30px;
    margin-top: 30px;
    .title {
        height: 50px;
        font-size: 30px;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-weight: 500;
        color: #333333;
        line-height: 50px;
        margin-top: 20px;
        text-align: center;
    }
    .cont {
        margin-top: 30px;
        .cont_li {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            padding-left: 100px;
            .cont_li_name {
                font-size: 14px;
                font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
                font-weight: 400;
                color: #333333;
                min-width: 100px;
                text-align: left;
                span {
                    color: #F10505;
                    font-size: 18px;
                    margin-right: 6px;
                }
            }
            .cont_li_input {
                margin-left: 10px;
                input {
                    width: 434px;
                    height: 38px;
                    border-radius: 4px;
                    border: 1px solid #ECEBEB;
                    text-indent: 1em;
                }
                &.captcha {
                    width: 434px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    input {
                        width: 293px;
                    }
                    span {
                        width: 127px;
                        height: 50px;
                        text-align: center;
                        line-height: 50px;
                        // border:1px solid #f7f7f7;
                        img {
                            width: 100%;
                            height: 100%;
                            display: block;
                        }
                    }
                }
                &.vercode {
                    position: relative;
                    span {
                        position: absolute;
                        right: 22px;
                        top:9px;
                        font-size: 14px;
                        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
                        font-weight: 400;
                        color: #006DC0;
                        &.downtimecount {
                            color: #999;
                        }
                    }
                }
            }
            .cont_li_tips {
                font-size: 12px;
                font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
                font-weight: 400;
                color: #999;
                margin-left: 20px;
            }
        }
    }
}
.resister_btn {
    width: 434px;
    height: 54px;
    background: linear-gradient(90deg, #3D9CED 0%, #1A60C7 99%, #1A60C7 100%);
    border-radius: 5px;
    font-size: 22px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-weight: 400;
    color: #FFFFFF;
    text-align: center;
    line-height: 54px;
    margin-top: 40px;
    margin-left: 210px;
}
</style>